#include "kernel/types.h"
#include "user/user.h"
#include "kernel/param.h"

#define MSGSIZE 16

int main(int argc, char *argv[]){
    sleep(5);
    char buf[MSGSIZE];
    read(0, buf, MSGSIZE);  //管道符号“|”通过文件描述符0传输

    char *xargv[MAXARG]; 
    int xargc = 0;
    for (int i = 1; i<argc; ++i) {
        xargv[xargc] = argv[i];
        xargc++;
    }

    char *p = buf;
    for (int i = 0; i < MSGSIZE; ++i) {
        if (buf[i] == '\n') {
            if (fork() > 0) {
                p = &buf[i+1];
                wait(0);
            } else {
                buf[i] = 0;
                xargv[xargc] = p;
                xargc++;
                xargv[xargc] = 0;
                xargc++;

                exec(xargv[0], xargv);
                
                exit(0);
            }
        }
    }
    wait(0);
    exit(0);
}

